package com.haynes.meetxiaohai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haynes.meetxiaohai.common.CustomException;
import com.haynes.meetxiaohai.dto.SetmealDto;
import com.haynes.meetxiaohai.entity.Setmeal;
import com.haynes.meetxiaohai.entity.SetmealDish;
import com.haynes.meetxiaohai.mapper.SetmealMapper;
import com.haynes.meetxiaohai.service.SetmealDishService;
import com.haynes.meetxiaohai.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author Haynes
 * @Date 2023/3/20 11:22
 * @PackageName:com.haynes.reggie.service.impl
 * @ClassName: SetmealServiceImpl
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper,Setmeal> implements SetmealService {
    @Autowired
    private SetmealDishService setmealDishService;

    @Override
    public void saveWitchDish(SetmealDto setmealDto) {
//        1.保存基本信息
        this.save(setmealDto);

//        2.获取到所有套餐内菜品信息，并一一赋值套餐ID
        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes();
        for (SetmealDish item : setmealDishList) {
            item.setSetmealId(setmealDto.getId());
        }
//      3.保存套餐，菜品信息
        setmealDishService.saveBatch(setmealDishList);
    }

    @Override
    public SetmealDto getByIdWitchDish(Long id) {
//        1.获取到当前套餐所有信息
        Setmeal setmeal = this.getById(id);

        SetmealDto setmealDto = new SetmealDto();
//        复制数据到ValueObject对象中
        BeanUtils.copyProperties(setmeal,setmealDto);

//      3.创建条件构造器
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getDishId,setmealDto.getId());
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getIsDeleted,0);
//      4.查询套餐表中的菜品信息
        List<SetmealDish> setmealDishList = setmealDishService.list(setmealDishLambdaQueryWrapper);
//        5.最后赋值给ValueObject对象中
        setmealDto.setSetmealDishes(setmealDishList);

        return setmealDto;
    }

    @Override
    public void updateWitchDish(SetmealDto dto) {
//        1.保存当前套餐基本信息
        this.updateById(dto);
//        2.删除当前套餐下的菜品信息
        LambdaQueryWrapper<SetmealDish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(SetmealDish::getDishId,dto.getId());
        setmealDishService.remove(dishLambdaQueryWrapper);
//        2.保存当前套餐下菜品信息
        List<SetmealDish> setmealDishList = dto.getSetmealDishes();
        for (SetmealDish item : setmealDishList) {
            item.setSetmealId(dto.getId());
        }
        setmealDishService.saveBatch(setmealDishList);
    }

    @Override
    @Transactional
    public void removeByIdBatch(List<Long> ids) {
        List<Setmeal> setmealList = this.listByIds(ids);
        for (Setmeal setmeal : setmealList) {
            if (setmeal.getStatus()==0){
                setmealDishService.removeByIds(ids);
                this.removeById(setmeal);
            }else {
                throw new CustomException("当前套餐正在售卖中无法删除");
            }
        }
    }

    @Override
    public void updateSetmealStatusByIdBatch(Integer status, List<Long> ids) {
        if (status!=null && ids!=null){
            List<Setmeal> setmealList = this.listByIds(ids);
            for (Setmeal setmeal : setmealList) {
                setmeal.setStatus(status);
                this.updateById(setmeal);
            }
        }
    }
}
